﻿unit UedgeWeb4DelphiOCX;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  {Winapi.Windows, Winapi.ActiveX, System.Classes, Vcl.Controls, Vcl.Graphics, Vcl.Menus, Vcl.Forms, Vcl.StdCtrls,
  System.Win.ComServ, StdVCL, AXCtrls, edgeWeb4Delphi_TLB, Vcl.ExtCtrls, Winapi.WebView2,
  Vcl.Edge, Vcl.ComCtrls, System.SysUtils,  Vcl.OleCtrls, Winapi.Messages, System.UITypes;}

  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Winapi.WebView2, Winapi.ActiveX,
  Vcl.StdCtrls, Vcl.Buttons, Vcl.Edge, Vcl.OleCtrls, SHDocVw, Vcl.ComCtrls,
  Vcl.AxCtrls, System.Win.StdVCL, System.Win.ComServ,edgeWeb4Delphi_TLB, Vcl.ExtCtrls ;

type
  TedgeWeb4DellphiOCX = class(TActiveXControl, IedgeWeb4DellphiOCX)
  private
    { Private declarations }
    FDelphiControl: TPanel;
    FEvents: IedgeWeb4DellphiOCXEvents;
    FExecReult:String;
    FEnableTabs:Boolean;
    FNavUrl:String;

    FPC: TPageControl;

    FCloseButtonsRect: array of TRect;
    FCloseButtonMouseDownIndex: Integer;
    FCloseButtonShowPushed: Boolean;

    procedure CanResizeEvent(Sender: TObject; var NewWidth, NewHeight: Integer; var Resize: Boolean);

    procedure ClickEvent(Sender: TObject);
    procedure ConstrainedResizeEvent(Sender: TObject; var MinWidth, MinHeight, MaxWidth,
          MaxHeight: Integer);
    procedure DblClickEvent(Sender: TObject);
    procedure MouseEnterEvent(Sender: TObject);
    procedure MouseLeaveEvent(Sender: TObject);
    procedure ResizeEvent(Sender: TObject);
    procedure OnExecuteScript(Sender: TCustomEdgeBrowser; AResult: HResult; const AResultObjectAsJson: string);

    procedure EdgeBrowserNewWindowRequested(Sender: TCustomEdgeBrowser;
      Args: TNewWindowRequestedEventArgs);
    procedure EdgeBrowserDocumentTitleChanged(Sender: TCustomEdgeBrowser;
      const ADocumentTitle: string);
    procedure PageControlDrawTab(Control: TCustomTabControl; TabIndex: Integer;
      const Rect: TRect; Active: Boolean);
    procedure PageControlMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure PageControlMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure PageControlMouseLeave(Sender: TObject);
    procedure PageControlMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);

    function NewTabs:TEdgeBrowser;
    procedure refreshTabCount;

  protected
    { Protected declarations }
    procedure DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage); override;
    procedure EventSinkChanged(const EventSink: IUnknown); override;
    procedure InitializeControl; override;
    function DrawTextBiDiModeFlagsReadingOnly: Integer; safecall;
    function Get_AlignDisabled: WordBool; safecall;
    function Get_Alignment: TxAlignment; safecall;
    function Get_AlignWithMargins: WordBool; safecall;
    function Get_AutoSize: WordBool; safecall;
    function Get_BevelInner: TxBevelCut; safecall;
    function Get_BevelKind: TxBevelKind; safecall;
    function Get_BevelOuter: TxBevelCut; safecall;
    function Get_BevelWidth: Integer; safecall;
    function Get_BorderStyle: TxBorderStyle; safecall;
    function Get_BorderWidth: Integer; safecall;
    function Get_Caption: WideString; safecall;
    function Get_Color: OLE_COLOR; safecall;
    function Get_Ctl3D: WordBool; safecall;
    function Get_CurrentPPI: Integer; safecall;
    function Get_DockSite: WordBool; safecall;
    function Get_DoubleBuffered: WordBool; safecall;
    function Get_DragCursor: Smallint; safecall;
    function Get_DragMode: TxDragMode; safecall;
    function Get_Enabled: WordBool; safecall;
    function Get_ExplicitHeight: Integer; safecall;
    function Get_ExplicitLeft: Integer; safecall;
    function Get_ExplicitTop: Integer; safecall;
    function Get_ExplicitWidth: Integer; safecall;
    function Get_Font: IFontDisp; safecall;
    function Get_FullRepaint: WordBool; safecall;
    function Get_IsDrawingLocked: WordBool; safecall;
    function Get_Locked: WordBool; safecall;
    function Get_MouseInClient: WordBool; safecall;
    function Get_ParentBackground: WordBool; safecall;
    function Get_ParentColor: WordBool; safecall;
    function Get_ParentCtl3D: WordBool; safecall;
    function Get_ParentCustomHint: WordBool; safecall;
    function Get_ParentDoubleBuffered: WordBool; safecall;
    function Get_PixelsPerInch: Integer; safecall;
    function Get_RaiseOnNonMainThreadUsage: WordBool; safecall;
    function Get_RedrawDisabled: WordBool; safecall;
    function Get_ScaleFactor: Single; safecall;
    function Get_ShowCaption: WordBool; safecall;
    function Get_StyleName: WideString; safecall;
    function Get_UseDockManager: WordBool; safecall;
    function Get_VerticalAlignment: TxVerticalAlignment; safecall;
    function Get_Visible: WordBool; safecall;
    function Get_VisibleDockClientCount: Integer; safecall;
    function GetStyleName: WideString; safecall;
    function GetSystemMetrics(nIndex: Integer): Integer; safecall;
    function IsCustomStyleActive: WordBool; safecall;
    function IsLightStyleColor(AColor: OLE_COLOR): WordBool; safecall;
    function IsRightToLeft: WordBool; safecall;
    function QualifiedClassName: WideString; safecall;
    function ScaleValue(param1: Integer): Integer; safecall;
    function UnitScope: WideString; safecall;
    function UseRightToLeftReading: WordBool; safecall;
    function UseRightToLeftScrollBar: WordBool; safecall;
    procedure _Set_Font(var Value: IFontDisp); safecall;
    procedure CheckNonMainThreadUsage; safecall;
    procedure DisposeOf; safecall;
    procedure InitiateAction; safecall;
    procedure LockDrawing; safecall;
    procedure ScaleForPPI(NewPPI: Integer); safecall;
    procedure Set_Alignment(Value: TxAlignment); safecall;
    procedure Set_AlignWithMargins(Value: WordBool); safecall;
    procedure Set_AutoSize(Value: WordBool); safecall;
    procedure Set_BevelInner(Value: TxBevelCut); safecall;
    procedure Set_BevelKind(Value: TxBevelKind); safecall;
    procedure Set_BevelOuter(Value: TxBevelCut); safecall;
    procedure Set_BevelWidth(Value: Integer); safecall;
    procedure Set_BorderStyle(Value: TxBorderStyle); safecall;
    procedure Set_BorderWidth(Value: Integer); safecall;
    procedure Set_Caption(const Value: WideString); safecall;
    procedure Set_Color(Value: OLE_COLOR); safecall;
    procedure Set_Ctl3D(Value: WordBool); safecall;
    procedure Set_DockSite(Value: WordBool); safecall;
    procedure Set_DoubleBuffered(Value: WordBool); safecall;
    procedure Set_DragCursor(Value: Smallint); safecall;
    procedure Set_DragMode(Value: TxDragMode); safecall;
    procedure Set_Enabled(Value: WordBool); safecall;
    procedure Set_Font(const Value: IFontDisp); safecall;
    procedure Set_FullRepaint(Value: WordBool); safecall;
    procedure Set_Locked(Value: WordBool); safecall;
    procedure Set_ParentBackground(Value: WordBool); safecall;
    procedure Set_ParentColor(Value: WordBool); safecall;
    procedure Set_ParentCtl3D(Value: WordBool); safecall;
    procedure Set_ParentCustomHint(Value: WordBool); safecall;
    procedure Set_ParentDoubleBuffered(Value: WordBool); safecall;
    procedure Set_PixelsPerInch(Value: Integer); safecall;
    procedure Set_RaiseOnNonMainThreadUsage(Value: WordBool); safecall;
    procedure Set_ShowCaption(Value: WordBool); safecall;
    procedure Set_StyleName(const Value: WideString); safecall;
    procedure Set_UseDockManager(Value: WordBool); safecall;
    procedure Set_VerticalAlignment(Value: TxVerticalAlignment); safecall;
    procedure Set_Visible(Value: WordBool); safecall;
    procedure SetDesignVisible(Value: WordBool); safecall;
    procedure SetSubComponent(IsSubComponent: WordBool); safecall;
    procedure UnlockDrawing; safecall;
    procedure Nav(const url: WideString); safecall;
    function  getScriptResult: WideString; safecall;
    procedure execScript(const script: WideString); safecall;
    function Get_EnableTabs: WordBool; safecall;
    procedure Set_EnableTabs(Value: WordBool); safecall;
  end;

implementation

uses ComObj, ImgList;

{ TedgeWeb4DellphiOCX }

procedure TedgeWeb4DellphiOCX.DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage);
begin
  {TODO: Define property pages here.  Property pages are defined by calling
    DefinePropertyPage with the class id of the page.  For example,
      DefinePropertyPage(Class_edgeWeb4DellphiOCXPage); }
end;

procedure TedgeWeb4DellphiOCX.EdgeBrowserDocumentTitleChanged(
  Sender: TCustomEdgeBrowser; const ADocumentTitle: string);
begin
  ((Sender).parent as TTabsheet).caption:=ADocumentTitle;
end;

procedure TedgeWeb4DellphiOCX.EdgeBrowserNewWindowRequested(
  Sender: TCustomEdgeBrowser; Args: TNewWindowRequestedEventArgs);
var
  PUri: PWideChar;
  Url: string;
begin
  if FEnableTabs then begin
    Args.ArgsInterface.Get_uri(PUri);
    Url := PUri;
    CoTaskMemFree(PUri);

    NewTabs.Navigate(Url);

    Args.ArgsInterface.Set_Handled(1);
  end;

  //新页面在当前窗口继续打开
  //Args.ArgsInterface.Set_NewWindow(Sender.DefaultInterface);
end;

procedure TedgeWeb4DellphiOCX.EventSinkChanged(const EventSink: IUnknown);
begin
  FEvents := EventSink as IedgeWeb4DellphiOCXEvents;
end;

procedure TedgeWeb4DellphiOCX.InitializeControl;
begin
  FDelphiControl := Control as TPanel;
  FDelphiControl.OnCanResize := CanResizeEvent;
  FDelphiControl.OnClick := ClickEvent;
  FDelphiControl.OnConstrainedResize := ConstrainedResizeEvent;
  FDelphiControl.OnDblClick := DblClickEvent;
  FDelphiControl.OnMouseEnter := MouseEnterEvent;
  FDelphiControl.OnMouseLeave := MouseLeaveEvent;
  FDelphiControl.OnResize := ResizeEvent;

  FEnableTabs:=False;

  FPC:=TPageControl.Create(FDelphiControl);
  FPC.Align:=alClient;
  FPC.Parent:=FDelphiControl;
  FPC.Visible:=True;
  FPC.Font.Name:='宋体';
  FPC.Font.Size:=9;
  FPC.Font.Charset:=GB2312_CHARSET;
  FPC.TabWidth:=100;
  FPC.TabHeight:=20;
  FPC.OnMouseDown:=PageControlMouseDown;
  FPC.OnMouseMove:=PageControlMouseMove;
  FPC.OnMouseLeave:=PageControlMouseLeave;
  FPC.OnMouseMove:=PageControlMouseMove;
  FPC.OnMouseUp:=PageControlMouseUp;
  FPC.OnDrawTab:=PageControlDrawTab;
  FPC.OwnerDraw := True;

  //should be done on every change of the page count
  refreshTabCount;
  FNavUrl:='about:blank';
  NewTabs.Navigate(FNavUrl);
end;

function TedgeWeb4DellphiOCX.DrawTextBiDiModeFlagsReadingOnly: Integer;
begin
  Result := FDelphiControl.DrawTextBiDiModeFlagsReadingOnly;
end;

function TedgeWeb4DellphiOCX.Get_AlignDisabled: WordBool;
begin
  Result := FDelphiControl.AlignDisabled;
end;

function TedgeWeb4DellphiOCX.Get_Alignment: TxAlignment;
begin
  Result := Ord(FDelphiControl.Alignment);
end;

function TedgeWeb4DellphiOCX.Get_AlignWithMargins: WordBool;
begin
  Result := FDelphiControl.AlignWithMargins;
end;

function TedgeWeb4DellphiOCX.Get_AutoSize: WordBool;
begin
  Result := FDelphiControl.AutoSize;
end;

function TedgeWeb4DellphiOCX.Get_BevelInner: TxBevelCut;
begin
  Result := Ord(FDelphiControl.BevelInner);
end;

function TedgeWeb4DellphiOCX.Get_BevelKind: TxBevelKind;
begin
  Result := Ord(FDelphiControl.BevelKind);
end;

function TedgeWeb4DellphiOCX.Get_BevelOuter: TxBevelCut;
begin
  Result := Ord(FDelphiControl.BevelOuter);
end;

function TedgeWeb4DellphiOCX.Get_BevelWidth: Integer;
begin
  Result := Integer(FDelphiControl.BevelWidth);
end;

function TedgeWeb4DellphiOCX.Get_BorderStyle: TxBorderStyle;
begin
  Result := Ord(FDelphiControl.BorderStyle);
end;

function TedgeWeb4DellphiOCX.Get_BorderWidth: Integer;
begin
  Result := Integer(FDelphiControl.BorderWidth);
end;

function TedgeWeb4DellphiOCX.Get_Caption: WideString;
begin
  Result := WideString(FDelphiControl.Caption);
end;

function TedgeWeb4DellphiOCX.Get_Color: OLE_COLOR;
begin
  Result := OLE_COLOR(FDelphiControl.Color);
end;

function TedgeWeb4DellphiOCX.Get_Ctl3D: WordBool;
begin
  Result := FDelphiControl.Ctl3D;
end;

function TedgeWeb4DellphiOCX.Get_CurrentPPI: Integer;
begin
  Result := FDelphiControl.CurrentPPI;
end;

function TedgeWeb4DellphiOCX.Get_DockSite: WordBool;
begin
  Result := FDelphiControl.DockSite;
end;

function TedgeWeb4DellphiOCX.Get_DoubleBuffered: WordBool;
begin
  Result := FDelphiControl.DoubleBuffered;
end;

function TedgeWeb4DellphiOCX.Get_DragCursor: Smallint;
begin
  Result := Smallint(FDelphiControl.DragCursor);
end;

function TedgeWeb4DellphiOCX.Get_DragMode: TxDragMode;
begin
  Result := Ord(FDelphiControl.DragMode);
end;

function TedgeWeb4DellphiOCX.Get_Enabled: WordBool;
begin
  Result := FDelphiControl.Enabled;
end;

function TedgeWeb4DellphiOCX.Get_ExplicitHeight: Integer;
begin
  Result := FDelphiControl.ExplicitHeight;
end;

function TedgeWeb4DellphiOCX.Get_ExplicitLeft: Integer;
begin
  Result := FDelphiControl.ExplicitLeft;
end;

function TedgeWeb4DellphiOCX.Get_ExplicitTop: Integer;
begin
  Result := FDelphiControl.ExplicitTop;
end;

function TedgeWeb4DellphiOCX.Get_ExplicitWidth: Integer;
begin
  Result := FDelphiControl.ExplicitWidth;
end;

function TedgeWeb4DellphiOCX.Get_Font: IFontDisp;
begin
  GetOleFont(FDelphiControl.Font, Result);
end;

function TedgeWeb4DellphiOCX.Get_FullRepaint: WordBool;
begin
  Result := FDelphiControl.FullRepaint;
end;

function TedgeWeb4DellphiOCX.Get_IsDrawingLocked: WordBool;
begin
  Result := FDelphiControl.IsDrawingLocked;
end;

function TedgeWeb4DellphiOCX.Get_Locked: WordBool;
begin
  Result := FDelphiControl.Locked;
end;

function TedgeWeb4DellphiOCX.Get_MouseInClient: WordBool;
begin
  Result := FDelphiControl.MouseInClient;
end;

function TedgeWeb4DellphiOCX.Get_ParentBackground: WordBool;
begin
  Result := FDelphiControl.ParentBackground;
end;

function TedgeWeb4DellphiOCX.Get_ParentColor: WordBool;
begin
  Result := FDelphiControl.ParentColor;
end;

function TedgeWeb4DellphiOCX.Get_ParentCtl3D: WordBool;
begin
  Result := FDelphiControl.ParentCtl3D;
end;

function TedgeWeb4DellphiOCX.Get_ParentCustomHint: WordBool;
begin
  Result := FDelphiControl.ParentCustomHint;
end;

function TedgeWeb4DellphiOCX.Get_ParentDoubleBuffered: WordBool;
begin
  Result := FDelphiControl.ParentDoubleBuffered;
end;

function TedgeWeb4DellphiOCX.Get_PixelsPerInch: Integer;
begin
  Result := FDelphiControl.PixelsPerInch;
end;

function TedgeWeb4DellphiOCX.Get_RaiseOnNonMainThreadUsage: WordBool;
begin
  Result := FDelphiControl.RaiseOnNonMainThreadUsage;
end;

function TedgeWeb4DellphiOCX.Get_RedrawDisabled: WordBool;
begin
  Result := FDelphiControl.RedrawDisabled;
end;

function TedgeWeb4DellphiOCX.Get_ScaleFactor: Single;
begin
  Result := FDelphiControl.ScaleFactor;
end;

function TedgeWeb4DellphiOCX.Get_ShowCaption: WordBool;
begin
  Result := FDelphiControl.ShowCaption;
end;

function TedgeWeb4DellphiOCX.Get_StyleName: WideString;
begin
  Result := WideString(FDelphiControl.StyleName);
end;

function TedgeWeb4DellphiOCX.Get_UseDockManager: WordBool;
begin
  Result := FDelphiControl.UseDockManager;
end;

function TedgeWeb4DellphiOCX.Get_VerticalAlignment: TxVerticalAlignment;
begin
  Result := Ord(FDelphiControl.VerticalAlignment);
end;

function TedgeWeb4DellphiOCX.Get_Visible: WordBool;
begin
  Result := FDelphiControl.Visible;
end;

function TedgeWeb4DellphiOCX.Get_VisibleDockClientCount: Integer;
begin
  Result := FDelphiControl.VisibleDockClientCount;
end;

function TedgeWeb4DellphiOCX.GetStyleName: WideString;
begin
  Result := FDelphiControl.GetStyleName;
end;

function TedgeWeb4DellphiOCX.GetSystemMetrics(nIndex: Integer): Integer;
begin
  Result := FDelphiControl.GetSystemMetrics(nIndex);
end;

function TedgeWeb4DellphiOCX.IsCustomStyleActive: WordBool;
begin
  Result := FDelphiControl.IsCustomStyleActive;
end;

function TedgeWeb4DellphiOCX.IsLightStyleColor(AColor: OLE_COLOR): WordBool;
begin

end;

function TedgeWeb4DellphiOCX.IsRightToLeft: WordBool;
begin
  Result := FDelphiControl.IsRightToLeft;
end;

function TedgeWeb4DellphiOCX.QualifiedClassName: WideString;
begin
  Result := FDelphiControl.QualifiedClassName;
end;

procedure TedgeWeb4DellphiOCX.refreshTabCount;
var
  i:Integer;
begin
  //刷新tabs数量，用于显示关闭按钮以及点击按钮时可以正确关闭
  SetLength(FCloseButtonsRect, FPC.PageCount);
  for I := 0 to Length(FCloseButtonsRect) - 1 do begin
    FCloseButtonsRect[I] := Rect(0, 0, 0, 0);
  end;
end;

function TedgeWeb4DellphiOCX.ScaleValue(param1: Integer): Integer;
begin
  Result := FDelphiControl.ScaleValue(param1);
end;

function TedgeWeb4DellphiOCX.UnitScope: WideString;
begin
  Result := FDelphiControl.UnitScope;
end;

function TedgeWeb4DellphiOCX.UseRightToLeftReading: WordBool;
begin
  Result := FDelphiControl.UseRightToLeftReading;
end;

function TedgeWeb4DellphiOCX.UseRightToLeftScrollBar: WordBool;
begin
  Result := FDelphiControl.UseRightToLeftScrollBar;
end;

procedure TedgeWeb4DellphiOCX._Set_Font(var Value: IFontDisp);
begin
  SetOleFont(FDelphiControl.Font, Value);
end;

procedure TedgeWeb4DellphiOCX.CanResizeEvent(Sender: TObject; var NewWidth, NewHeight: Integer;
          var Resize: Boolean);
var
  TempNewWidth: Integer;
  TempNewHeight: Integer;
  TempResize: WordBool;
begin
  TempNewWidth := Integer(NewWidth);
  TempNewHeight := Integer(NewHeight);
  TempResize := WordBool(Resize);
  if FEvents <> nil then FEvents.OnCanResize(TempNewWidth, TempNewHeight, TempResize);
  NewWidth := Integer(TempNewWidth);
  NewHeight := Integer(TempNewHeight);
  Resize := Boolean(TempResize);
end;

procedure TedgeWeb4DellphiOCX.CheckNonMainThreadUsage;
begin
  FDelphiControl.CheckNonMainThreadUsage;
end;

procedure TedgeWeb4DellphiOCX.ClickEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnClick;
end;

procedure TedgeWeb4DellphiOCX.ConstrainedResizeEvent(Sender: TObject; var MinWidth,
          MinHeight, MaxWidth, MaxHeight: Integer);
var
  TempMinWidth: Integer;
  TempMinHeight: Integer;
  TempMaxWidth: Integer;
  TempMaxHeight: Integer;
begin
  TempMinWidth := Integer(MinWidth);
  TempMinHeight := Integer(MinHeight);
  TempMaxWidth := Integer(MaxWidth);
  TempMaxHeight := Integer(MaxHeight);
  if FEvents <> nil then FEvents.OnConstrainedResize(TempMinWidth, TempMinHeight, TempMaxWidth, TempMaxHeight);
  MinWidth := Integer(TempMinWidth);
  MinHeight := Integer(TempMinHeight);
  MaxWidth := Integer(TempMaxWidth);
  MaxHeight := Integer(TempMaxHeight);
end;

procedure TedgeWeb4DellphiOCX.DblClickEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnDblClick;
end;

procedure TedgeWeb4DellphiOCX.DisposeOf;
begin
  FDelphiControl.DisposeOf;
end;

procedure TedgeWeb4DellphiOCX.InitiateAction;
begin
  FDelphiControl.InitiateAction;
end;

procedure TedgeWeb4DellphiOCX.LockDrawing;
begin
  FDelphiControl.LockDrawing;
end;

procedure TedgeWeb4DellphiOCX.MouseEnterEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnMouseEnter;
end;

procedure TedgeWeb4DellphiOCX.MouseLeaveEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnMouseLeave;
end;

procedure TedgeWeb4DellphiOCX.ResizeEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnResize;
end;

procedure TedgeWeb4DellphiOCX.ScaleForPPI(NewPPI: Integer);
begin
  FDelphiControl.ScaleForPPI(NewPPI);
end;

procedure TedgeWeb4DellphiOCX.Set_Alignment(Value: TxAlignment);
begin
  FDelphiControl.Alignment := TAlignment(Value);
end;

procedure TedgeWeb4DellphiOCX.Set_AlignWithMargins(Value: WordBool);
begin
  FDelphiControl.AlignWithMargins := Value;
end;

procedure TedgeWeb4DellphiOCX.Set_AutoSize(Value: WordBool);
begin
  FDelphiControl.AutoSize := Value;
end;

procedure TedgeWeb4DellphiOCX.Set_BevelInner(Value: TxBevelCut);
begin
  FDelphiControl.BevelInner := TBevelCut(Value);
end;

procedure TedgeWeb4DellphiOCX.Set_BevelKind(Value: TxBevelKind);
begin
  FDelphiControl.BevelKind := TBevelKind(Value);
end;

procedure TedgeWeb4DellphiOCX.Set_BevelOuter(Value: TxBevelCut);
begin
  FDelphiControl.BevelOuter := TBevelCut(Value);
end;

procedure TedgeWeb4DellphiOCX.Set_BevelWidth(Value: Integer);
begin
  FDelphiControl.BevelWidth := TBevelWidth(Value);
end;

procedure TedgeWeb4DellphiOCX.Set_BorderStyle(Value: TxBorderStyle);
begin
  FDelphiControl.BorderStyle := TBorderStyle(Value);
end;

procedure TedgeWeb4DellphiOCX.Set_BorderWidth(Value: Integer);
begin
  FDelphiControl.BorderWidth := TBorderWidth(Value);
end;

procedure TedgeWeb4DellphiOCX.Set_Caption(const Value: WideString);
begin
  FDelphiControl.Caption := TCaption(Value);
end;

procedure TedgeWeb4DellphiOCX.Set_Color(Value: OLE_COLOR);
begin
  FDelphiControl.Color := TColor(Value);
end;

procedure TedgeWeb4DellphiOCX.Set_Ctl3D(Value: WordBool);
begin
  FDelphiControl.Ctl3D := Value;
end;

procedure TedgeWeb4DellphiOCX.Set_DockSite(Value: WordBool);
begin
  FDelphiControl.DockSite := Value;
end;

procedure TedgeWeb4DellphiOCX.Set_DoubleBuffered(Value: WordBool);
begin
  FDelphiControl.DoubleBuffered := Value;
end;

procedure TedgeWeb4DellphiOCX.Set_DragCursor(Value: Smallint);
begin
  FDelphiControl.DragCursor := TCursor(Value);
end;

procedure TedgeWeb4DellphiOCX.Set_DragMode(Value: TxDragMode);
begin
  FDelphiControl.DragMode := TDragMode(Value);
end;

procedure TedgeWeb4DellphiOCX.Set_Enabled(Value: WordBool);
begin
  FDelphiControl.Enabled := Value;
end;

procedure TedgeWeb4DellphiOCX.Set_Font(const Value: IFontDisp);
begin
  SetOleFont(FDelphiControl.Font, Value);
end;

procedure TedgeWeb4DellphiOCX.Set_FullRepaint(Value: WordBool);
begin
  FDelphiControl.FullRepaint := Value;
end;

procedure TedgeWeb4DellphiOCX.Set_Locked(Value: WordBool);
begin
  FDelphiControl.Locked := Value;
end;

procedure TedgeWeb4DellphiOCX.Set_ParentBackground(Value: WordBool);
begin
  FDelphiControl.ParentBackground := Value;
end;

procedure TedgeWeb4DellphiOCX.Set_ParentColor(Value: WordBool);
begin
  FDelphiControl.ParentColor := Value;
end;

procedure TedgeWeb4DellphiOCX.Set_ParentCtl3D(Value: WordBool);
begin
  FDelphiControl.ParentCtl3D := Value;
end;

procedure TedgeWeb4DellphiOCX.Set_ParentCustomHint(Value: WordBool);
begin
  FDelphiControl.ParentCustomHint := Value;
end;

procedure TedgeWeb4DellphiOCX.Set_ParentDoubleBuffered(Value: WordBool);
begin
  FDelphiControl.ParentDoubleBuffered := Value;
end;

procedure TedgeWeb4DellphiOCX.Set_PixelsPerInch(Value: Integer);
begin
  FDelphiControl.PixelsPerInch := Value;
end;

procedure TedgeWeb4DellphiOCX.Set_RaiseOnNonMainThreadUsage(Value: WordBool);
begin
  FDelphiControl.RaiseOnNonMainThreadUsage := Value;
end;

procedure TedgeWeb4DellphiOCX.Set_ShowCaption(Value: WordBool);
begin
  FDelphiControl.ShowCaption := Value;
end;

procedure TedgeWeb4DellphiOCX.Set_StyleName(const Value: WideString);
begin
  FDelphiControl.StyleName := string(Value);
end;

procedure TedgeWeb4DellphiOCX.Set_UseDockManager(Value: WordBool);
begin
  FDelphiControl.UseDockManager := Value;
end;

procedure TedgeWeb4DellphiOCX.Set_VerticalAlignment(Value: TxVerticalAlignment);
begin
  FDelphiControl.VerticalAlignment := TVerticalAlignment(Value);
end;

procedure TedgeWeb4DellphiOCX.Set_Visible(Value: WordBool);
begin
  FDelphiControl.Visible := Value;
end;

procedure TedgeWeb4DellphiOCX.SetDesignVisible(Value: WordBool);
begin
  FDelphiControl.SetDesignVisible(Value);
end;

procedure TedgeWeb4DellphiOCX.SetSubComponent(IsSubComponent: WordBool);
begin
  FDelphiControl.SetSubComponent(IsSubComponent);
end;

procedure TedgeWeb4DellphiOCX.UnlockDrawing;
begin
  FDelphiControl.UnlockDrawing;
end;

procedure TedgeWeb4DellphiOCX.Nav(const url: WideString);
begin
  FNavUrl:=url;
  //关闭其它的页签，只保留一个
  while FPC.PageCount>1 do
    FPC.Pages[FPC.PageCount -1].Free;

  refreshTabCount;

  (FPC.Pages[0].Components[0] as TEdgeBrowser).Navigate(FNavUrl);
end;

function TedgeWeb4DellphiOCX.NewTabs: TEdgeBrowser;
var
  Web:TEdgeBrowser;
  tab:TTabSheet;
  I:Integer;
begin
  tab:=TTabSheet.Create(FPC);
  tab.Parent:=FPC;
  tab.Caption:=' ';
  Web:=TEdgeBrowser.create(tab);
  Web.Parent:=tab;
  Web.Align:=alClient;
  Web.Visible:=True;
  Web.OnDocumentTitleChanged:=EdgeBrowserDocumentTitleChanged;
  Web.OnNewWindowRequested:=EdgeBrowserNewWindowRequested;
  tab.PageControl:=FPC;
  tab.TabVisible:=FEnableTabs;
  FPC.ActivePage:=tab;

  refreshTabCount;

  FPC.Repaint;
  Result:=Web;
end;

procedure TedgeWeb4DellphiOCX.OnExecuteScript(Sender: TCustomEdgeBrowser;
  AResult: HResult; const AResultObjectAsJson: string);
begin
  FExecReult:=AResultObjectAsJson;
end;

procedure TedgeWeb4DellphiOCX.PageControlDrawTab(Control: TCustomTabControl;
  TabIndex: Integer; const Rect: TRect; Active: Boolean);
var
  CloseBtnSize: Integer;
  PageControl: TPageControl;
  TabCaption: TPoint;
  CloseBtnRect: TRect;
  TabCaptionRect: TRect;
  CloseBtnDrawState: Cardinal;
begin
  PageControl := Control as TPageControl;

  if (TabIndex>=0) and (TabIndex<=(Length(FCloseButtonsRect) - 1)) then  begin
    CloseBtnSize := 14;
    TabCaption.Y := Rect.Top + 3;

    if Active then
    begin
      CloseBtnRect.Top := Rect.Top + 4;
      CloseBtnRect.Right := Rect.Right - 5;
      TabCaption.X := Rect.Left + 6;
    end
    else
    begin
      CloseBtnRect.Top := Rect.Top + 3;
      CloseBtnRect.Right := Rect.Right - 5;
      TabCaption.X := Rect.Left + 3;
    end;

    CloseBtnRect.Bottom := CloseBtnRect.Top + CloseBtnSize;
    CloseBtnRect.Left := CloseBtnRect.Right - CloseBtnSize;
    FCloseButtonsRect[TabIndex] := CloseBtnRect;

    PageControl.Canvas.FillRect(Rect);
    TabCaptionRect.Left   := Rect.Left;
    TabCaptionRect.Top    := Rect.Top;
    TabCaptionRect.Width  := Rect.Width - CloseBtnSize - 10;
    TabCaptionRect.Height := Rect.Height;
    PageControl.Canvas.TextRect(TabCaptionRect,
      TabCaption.X, TabCaption.Y + 2, PageControl.Pages[TabIndex].Caption);

      if (FCloseButtonMouseDownIndex = TabIndex) and FCloseButtonShowPushed then
        CloseBtnDrawState := DFCS_CAPTIONCLOSE + DFCS_PUSHED
      else
        CloseBtnDrawState := DFCS_CAPTIONCLOSE;

    Winapi.Windows.DrawFrameControl(PageControl.Canvas.Handle,
      FCloseButtonsRect[TabIndex], DFC_CAPTION, CloseBtnDrawState);
  end;
end;

procedure TedgeWeb4DellphiOCX.PageControlMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  I: Integer;
  PageControl: TPageControl;
begin
  PageControl := Sender as TPageControl;

  if (Button = TMouseButton.mbLeft) then
  begin
    for I := 0 to Length(FCloseButtonsRect) - 1 do
    begin
      if PtInRect(FCloseButtonsRect[I], Point(X, Y)) then
      begin
        FCloseButtonMouseDownIndex := I;
        FCloseButtonShowPushed := True;
        PageControl.Repaint;
      end;
    end;
  end;
end;

procedure TedgeWeb4DellphiOCX.PageControlMouseLeave(Sender: TObject);
var
  PageControl: TPageControl;
begin
  PageControl := Sender as TPageControl;
  FCloseButtonShowPushed := False;
  PageControl.Repaint;
end;

procedure TedgeWeb4DellphiOCX.PageControlMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
var
  PageControl: TPageControl;
  Inside: Boolean;
begin
  PageControl := Sender as TPageControl;

  if (ssLeft in Shift) and (FCloseButtonMouseDownIndex >= 0) then
  begin
    Inside := PtInRect(FCloseButtonsRect[FCloseButtonMouseDownIndex], Point(X, Y));

    if FCloseButtonShowPushed <> Inside then
    begin
      FCloseButtonShowPushed := Inside;
      PageControl.Repaint;
    end;
  end;
end;

procedure TedgeWeb4DellphiOCX.PageControlMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  PageControl: TPageControl;
  I:Integer;
begin
  PageControl := Sender as TPageControl;

  if (Button = TMouseButton.mbLeft) and (FCloseButtonMouseDownIndex >= 0) then
  begin
    if PtInRect(FCloseButtonsRect[FCloseButtonMouseDownIndex], Point(X, Y)) then
    begin
      //ShowMessage('Button ' + IntToStr(FCloseButtonMouseDownIndex + 1) + ' pressed!');
      if PageControl.PageCount>1 then begin
        PageControl.Pages[FCloseButtonMouseDownIndex].Free;
        refreshTabCount;
      end else
        (PageControl.Pages[0].Components[0] as TEdgeBrowser).Navigate(FNavUrl);

      FCloseButtonMouseDownIndex := -1;
      PageControl.Repaint;
    end;
  end;
end;

function TedgeWeb4DellphiOCX.getScriptResult: WideString;
begin
  Result:=self.FExecReult;
end;

procedure TedgeWeb4DellphiOCX.execScript(const script: WideString);
var
  web:TEdgeBrowser;
begin
  web:=(FPC.ActivePage.Components[0] as TEdgeBrowser);
  Web.OnExecuteScript:=self.OnExecuteScript;
  Web.ExecuteScript(script);
end;

function TedgeWeb4DellphiOCX.Get_EnableTabs: WordBool;
begin
  Result:=FEnableTabs;
end;

procedure TedgeWeb4DellphiOCX.Set_EnableTabs(Value: WordBool);
var I:Integer;
begin
  FEnableTabs:=Value;
  for I := 0 to FPC.PageCount -1 do
    FPC.Pages[I].TabVisible:=FEnableTabs;
end;

initialization
  TActiveXControlFactory.Create(
    ComServer,
    TedgeWeb4DellphiOCX,
    TPanel,
    Class_edgeWeb4DellphiOCX,
    0,
    '',
    0,
    tmApartment);
end.
